import 'dart:io';
//原地址：https://www.jianshu.com/p/fe98bf64cafd
main() async {
  var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 9988);
  await for (var request in server) {
    var accessControlAllowOrigin = [
      'http://localhost:63062',
      'http://localhost:55355'
    ];
    if (request.headers['origin'] != null) {
      for (String tempAllowOrigin in accessControlAllowOrigin) {
        if (request.headers['origin'].first.contains(tempAllowOrigin)) {
          request.response
            ..headers
                .add('Access-Control-Allow-Origin', request.headers['origin'])
          // ..headers.add('Access-Control-Allow-Origin', '*')
            ..headers.contentType =
            ContentType('text', 'plain', charset: 'utf-8')
//            ..write('Hello Dart! 你好Dart 跨域')
            ..close();
        }
      }
    } else {
      request.response
        ..headers.contentType = ContentType('text', 'plain', charset: 'utf-8')
//        ..write('Hello Dart! 你好Dart 不需要跨域')
        ..close();
    }
  }
}